<%--
  Created by IntelliJ IDEA.
  User: 29460
  Date: 2020/11/9
  Time: 13:43
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <link rel="stylesheet" href="./layui/css/layui.css">
    <script src="js/jquery-3.2.1.js"></script>
    <script src="layui/layui.js"></script>
</head>
<body>
<div style="width: 100%; margin: 0 auto" >
    <table id="showAll" lay-filter="test"></table>
</div>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
</body>
<script>
    layui.use(['table','layer','form'], function(){
        var table = layui.table;
        var form = layui.form;
        var layer= layui.layer;
    var tab= table.render({
        elem: '#showAll'
        ,height:500
        ,url: 'tb/list' //数据接口
        ,page: true //开启分页
        ,toolbar: '#deletebar' //开启头部工具栏，并为其绑定左侧模板
        ,defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
            title: '提示'
            ,layEvent: 'LAYTABLE_TIPS'
            ,icon: 'layui-icon-tips'
        }]
        ,limit:3
        ,limits:[3,6,9]
        ,cols: [[ //表头
            {type: 'checkbox', fixed: 'left' ,title:"全选/全不选",width:30},
            {field: 'id', title: '编号', sort: true, fixed: 'left'}
            ,{field: 'categoryId', title: '分类id'}
            ,{field: 'title', title: '标题'}
            ,{field: 'subTitle', title: '小标题'}
            ,{field: 'titleDesc', title: '标题描述'}
            ,{field: 'pic', title: '照片1'}
            ,{field: 'pic2', title: '照片2'}
            ,{field: 'content', title: '内容'}
            ,{field: 'updated', title: '更新时间'}
            ,{field: 'created', title: '创建时间'}
            ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:150}
        ]]
        ,request: {
            pageName: 'page' //页码的参数名称，默认：page
            ,limitName: 'pageSize' //每页数据量的参数名，默认：limit
        }
    });
        table.on("tool(test)",function (obj) {
            var id = obj.data.mid;
            console.log(id);
            if (obj.event==='edit'){
                $.ajax({
                    type:'post',
                    dataType:'json',
                    url:'major/findById?id='+id,
                    success:function (resp) {//表赋值
                        form.val('updateFormFilter',{
                            mid:resp.major.mid,
                            mname:resp.major.mname,
                            credit:resp.major.credit,
                            lifeyear:resp.major.lifeyear,
                            introduction:resp.major.introduction,
                            cid:resp.major.cid
                        })
                    }
                });//打开窗口
                layer.open({
                    type:1,
                    title:'修改信息',
                    area:['800px', '500px'],
                    content:$("#updateMajor")//dom节点
                });
            }else {layer.confirm("确定删除吗", {btn:['狠心删除', '算了，不删了'],icon: 3},function () {
                $.ajax({
                    type:'post',
                    dataType:'json',
                    url:'major/deleteById?id='+id,
                    success:function (resp) {
                        tab.reload({page:true});
                        layer.msg(resp.msg)
                    }
                })
            });

            }
        });


    })
</script>
</html>
